﻿using System;
using System.IO;

namespace ECommon
{
    /// <summary>
    /// 文件工具
    /// </summary>
    public static partial class IoTool
    {
        /// <summary>
        /// 获取全路径的目录
        /// </summary>
        /// <param name="fullPath">全路径</param>
        /// <returns>目录</returns>
        public static string GetDirectory(string fullPath)
        {
            return Path.GetDirectoryName(fullPath);
        }

        /// <summary>
        /// 创建路径的目录
        /// </summary>
        /// <param name="path">路径</param>
        public static void CreateDirectory(string path)
        {
            string fullPath = Path.GetFullPath(path);
            string directory = GetDirectory(fullPath);
            if (string.IsNullOrEmpty(directory))
            {
                return;
            }
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }

        /// <summary>
        /// 验证文件是否存在
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <exception cref="ArgumentNullException">参数为空异常</exception>
        /// <exception cref="FileNotFoundException">文件不存在异常</exception>
        public static void FmtFilePathAndExistCheck(ref string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath", "文件路径为空");
            }
            filePath = filePath.Replace('\\', Path.DirectorySeparatorChar);
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException("文件不存在", filePath);
            }
        }
    }
}